package com.jelly.common.util.web;

import com.jelly.common.constant.ClientConstant;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @创建人 ql
 * @创建时间 2018-08-03 16:39
 * @功能描述: 应用级对象获取工具类
 **/
public class RequestContextHolderUtil {

    public static HttpServletRequest getRequest() {
        return getRequestAttributes().getRequest();
    }

    public static HttpServletResponse getResponse() {
        return getRequestAttributes().getResponse();
    }

    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    public static ServletRequestAttributes getRequestAttributes() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
    }

    public static ServletContext getServletContext() {
        return ContextLoader.getCurrentWebApplicationContext().getServletContext();
    }

    /**
     * 获取请求String参数
     * @param name
     * @return
     */
    public static String getHttpServletRequestString(String name) {
        return ServletRequestUtils.getStringParameter(getRequest(),name,"");
    }

    /**
     * 获取请求String参数 defaultVal默认值
     * @param name
     * @return
     */
    public static String getHttpServletRequestString(String name,String defaultVal) {
        return ServletRequestUtils.getStringParameter(getRequest(),name,defaultVal);
    }


    /**
     * 获取请求Integer参数
     * @param name
     * @return
     */
    public static Integer getHttpServletRequestInt(String name) {
        return ServletRequestUtils.getIntParameter(getRequest(),name,0);
    }

    /**
     * 获取请求Integer参数 defaultVal默认值
     * @param name
     * @return
     */
    public static Integer getHttpServletRequestInt(String name,Integer defaultVal) {
        return ServletRequestUtils.getIntParameter(getRequest(),name,defaultVal);
    }

    /**
     * 获取请求boolean参数
     * @param name
     * @return
     */
    public static Boolean getHttpServletRequestBoolen(String name) {
        return ServletRequestUtils.getBooleanParameter(getRequest(),name,false);
    }

    /**
     * 获取请求boolean参数 defaultVal默认值
     * @param name
     * @return
     */
    public static Boolean getHttpServletRequestBoolen(String name,boolean defaultVal) {
        return ServletRequestUtils.getBooleanParameter(getRequest(),name,defaultVal);
    }

    /**
     * 获取请求URL
     * @return
     */
    public static String getHttpServletRequestUrl() {
        return getRequest().getRequestURI();
    }

    /**
     * 获取客户端的IP地址。
     */
    public static String getIPAddress() {
        String ipv4 = getRequest().getHeader("X-Real-IP");
        if ((null != ipv4) && (ipv4.length() > 0)) {
            return ipv4;
        }
        return getRequest().getRemoteAddr();
    }


    public static String  mobileDevice(){
        /**
         * android : 所有android设备
         * mac os : iphone ipad
         * windows phone:Nokia等windows系统的手机
         */
        String requestHeader = getRequest().getHeader("user-agent");
        String[] deviceArray = new String[]{"android","mac os","windows phone"};
        if(requestHeader == null)
            return ClientConstant.RQUIPMENT;
        requestHeader = requestHeader.toLowerCase();
        for(int i=0;i<deviceArray.length;i++){
            if(requestHeader.indexOf(deviceArray[i])>0){
                return ClientConstant.ANDROID;
            }
        }
        return ClientConstant.PC;
    }
}
